import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_redis import FlaskRedis

# Initialize extensions
db = SQLAlchemy()
redis_client = FlaskRedis()

def create_app(config_name=None):
    app = Flask(__name__)
    
    # Load configuration
    app.config.from_object('app.config.Config')
    
    # Initialize extensions with app
    db.init_app(app)
    redis_client.init_app(app)
    
    # Register before request handler
    from app.middleware import before_request
    app.before_request(before_request)
    
    # Register blueprints
    from app.views.main import main_bp
    from app.views.auth import auth_bp
    from app.views.users import users_bp
    
    app.register_blueprint(main_bp)
    app.register_blueprint(auth_bp)
    app.register_blueprint(users_bp)
    
    return app

# Import Redis utilities
from app.redis_utils import RedisManager, RedisKeys

# Import trace utilities
from app.trace_utils import parse_trace_id, get_request_info, format_trace_log

# Import session utilities
from app.session_utils import SessionManager, require_auth